using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;

namespace Dracham.TileManagement.CharacterManagement.Animation
{
    public class Walk:ICloneable
    {
        private List<Image> images = new List<Image>();
        private String direction = "";
        private Int32 changeSpeed = default(Int32);
        private Double timeSinceLastChange = default(Double);
        private Image image;

        #region properties 

        public Int32 ChangeSpeed
        {
            get { return this.changeSpeed; }
        }

        public String Direction
        {
            get { return this.direction; }
        }

        public Image Image
        {
            get { return this.image; }
        }

        #endregion

        private Walk()
        {

        }

        public void Update(GameTime gameTime)
        {
            if (timeSinceLastChange < changeSpeed)
            {
                timeSinceLastChange += gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            else
            {
                Int32 cimg = images.IndexOf(image);

                if (cimg == images.Count - 1)
                {
                    cimg = 0;
                }
                else { cimg++; }

                this.image = this.images[cimg];
                timeSinceLastChange = 0;
            }
        }

        public static Walk FromXmlNode(XmlNode walkNode)
        {
            Walk w = new Walk();

            foreach (XmlNode node in walkNode.ChildNodes)
            {
                if (node.Name == "Image")
                {
                    w.images.Add(Image.FromXmlNode(node));
                }
            }

            w.changeSpeed = Convert.ToInt32(walkNode.Attributes["ChangeSpeed"].Value);
            w.direction = walkNode.Attributes["Direction"].Value;
            w.image = w.images[0];

            return w;
        }

        #region ICloneable Members

        public object Clone()
        {
            Walk w = new Walk();

            foreach (Image i in this.images)
            {
                w.images.Add((Image)i.Clone());
            }

            w.changeSpeed = this.changeSpeed;
            w.direction = this.direction;
            w.image = w.images[0];

            return w;
        }

        #endregion
    }
}
